package cn.zhl.algorithm._3_并查集.common;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: demos
 * @description: 通用并查集
 * @author: 刘振华
 * @create: 2020-06-12 13:16
 **/
public abstract class UnionFindCommon<V> {
	protected Map<V,Node<V>> nodes = new HashMap<>();
	public UnionFindCommon(V[] vs){
		for(int i = 0; i < vs.length; i++){
			nodes.put(vs[i],new Node<>(vs[i]));
		}
	}
	public abstract V find(V v);
	public abstract void union(V v1,V v2);
	public boolean isSame(V v1,V v2){
		return find(v1) == find(v2);
	}
	protected static class Node<V>{
		public int rank = 1;
		public V value;
		public Node<V> parent = this;
		public Node(V value){
			this.value = value;
		}

	}

}
